/**
 * @file    modbus_app.c
 * @brief   
 * @author  liuwei
 * @version 1.0.1
 * @date    2023-05-18
 * 
 * @copyright Copyright (c) 2023 JBD-Energy Storage Technology Co. LTD
 * 
 * @par 修改日志:
 * <table>
 * <tr><th>Date       <th>Version   <th>Author   <th>Description
 * <tr><td>2023-05-18 <td>1.0.1     <td>Liuwei   <td>首次创建
 * </table>
 */

/* includes ------------------------------------------------------------------*/
#include "modbus_app.h"
#include "mcu_type.h"
/* macro/define --------------------------------------------------------------*/

/* typedef -------------------------------------------------------------------*/

/* local functions -----------------------------------------------------------*/

/* static variables ----------------------------------------------------------*/

/* global variables ----------------------------------------------------------*/

/** ------------------------------------------------------------------------- *
  *                             Global function
 ** ------------------------------------------------------------------------- */
/**
 * @brief
 * @param
 * @return
*/
uint8_t modbus_rtu_analysis(uint8_t *rx_buf ,uint16_t rx_num , md_data_type *recv_data)
{
    uint16_t calc_crc = 0;
    uint16_t recv_crc = 0;
    uint16_t i = 0;
	
    calc_crc = modbus_calc_crc(rx_buf, rx_num - 2);
    recv_crc = rx_buf[rx_num - 2] + (rx_buf[rx_num - 1] << 8);
    if(calc_crc == recv_crc)
    {
        recv_data->addr = rx_buf[0];
        recv_data->cmd_code = rx_buf[1];
        recv_data->reg_addr = (rx_buf[2] << 8) + rx_buf[3];
        recv_data->data_len = (rx_buf[4] << 8) + rx_buf[5];
        
		if(recv_data->cmd_code == 0x10)
		{
			recv_data->data_size = rx_buf[6] >> 1;
			for( i = 0;(i < recv_data->data_len) && (i < RTU_DATA_SIZE); ++ i)
			{
				recv_data->data_buf[i] = (rx_buf[7 + i * 2] << 8) + rx_buf[8 + i * 2];
			}
		}
        return TRUE;
    }
    else
    {
        return FALSE;
    }
}

/** ------------------------------------------------------------------------- *
  *                              Local function
 ** ------------------------------------------------------------------------- */

/********** Copyright (c) 2023 JBD-Energy Storage Technology Co. LTD *********/


